﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using DTO;

namespace DAL
{
    public class UsuarioDal : AbstractDal
    {
        public bool Login(string login, string senha)
        {
            try
            {
                MySqlCommand cmd = new MySqlCommand("SELECT id FROM usuario WHERE login = @login AND senha = @senha");
                cmd.Parameters.AddWithValue("@login", login);
                cmd.Parameters.AddWithValue("@senha", senha);
                AbrirConexao();
                cmd.Connection = conn;
                MySqlDataReader reader = cmd.ExecuteReader();
                int id = 0;
                while (reader.Read())
                {
                    id = reader["id"] != DBNull.Value ? Convert.ToInt32(reader["id"]) : 0;
                }
                if (id == 0)
                    return false;
                else
                    return true;
            }
            catch(Exception ex)
            {
                Log.Write(ex, "UsuarioDal -> login");
                throw;
            }
            finally
            {
                FecharConexao();
            }
        }

        public int AlteraLogin(string login, string senha, string novoLogin)
        {
            try
            {
                string query = "UPDATE usuario SET login = @novoLogin where login = @login and senha = @senha";
                AbrirConexao();
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@login", login);
                cmd.Parameters.AddWithValue("@senha", senha);
                cmd.Parameters.AddWithValue("@novoLogin", novoLogin);
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                FecharConexao();
            }
        }
        public int AlteraSenha(string login, string senha, string novaSenha)
        {
            try
            {
                string query = "UPDATE usuario SET senha = @novaSenha where login = @login and senha = @senha";
                AbrirConexao();
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@login", login);
                cmd.Parameters.AddWithValue("@senha", senha);
                cmd.Parameters.AddWithValue("@novaSenha", novaSenha);
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                FecharConexao();
            }
        }
        public int AlteraLogineSenha(string login, string senha, string novoLogin, string novaSenha)
        {
            try
            {
                string query = "UPDATE usuario SET senha = @novaSenha, login = @novoLogin where login = @login and senha = @senha";
                AbrirConexao();
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@login", login);
                cmd.Parameters.AddWithValue("@senha", senha);
                cmd.Parameters.AddWithValue("@novaSenha", novaSenha);
                cmd.Parameters.AddWithValue("@novoLogin", novoLogin);
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                FecharConexao();
            }
        }

        public Usuario SelecionarAdmin()
        {

            try
            {
                MySqlCommand cmd = new MySqlCommand("SELECT * FROM usuario");
                AbrirConexao();
                cmd.Connection = conn;
                MySqlDataReader reader = cmd.ExecuteReader();
                if(reader.Read())
                {
                    Usuario us = new Usuario();
                    us.Login = reader["login"] == null ? null : (string)reader["login"];
                    us.Senha = reader["senha"] == null ? null : (string)reader["senha"];
                    us.Email = reader["email"] == null ? null : (string)reader["email"];

                    return us;
                }

                return null;
            }
            catch
            {
                throw;
            }
            finally
            {
                FecharConexao();
            }
        }
    }
}
